Ccay.Module.define("ccay.security.user.list",function(page,$S){
	// igrid对象
	var igridObj;
	
		
	/**
	 * 操作
	 */
	userOp=function(record) {
		var str = new Array();
		if (Ccay.Permission.check("permissionManage:userAuthz")) {
			str.push("<a class='ccay-icon key'  title='"+ $i18n("ccay.permission.label.addPermission")
							+ "' onClick='ccay.security.user.list.openPermission("+ record.id + ")'></a>");
		}else{
			str.push("<span style='margin-left:16px'></span>");
		}
		if(Ccay.Permission.check("permissionManage:findUserCurrentPermissions")){
			str.push("<a class='ccay-icon userpermission'  title='"+ $i18n("ccay.permission.label.listPermission")
				+ "' onClick='Ccay.security.user.ListUser.queryUserPermission("+ record.id +")'></a>");
		}else{
			str.push("<span style='margin-left:16px'></span>");
		}
		if(Ccay.Permission.check("userManagement:update")){
			str.push("<a class='ccay-icon edit'  title='"+ $i18n("ccay.common.button.edit")
				+ "' onClick='ccay.security.user.list.toEdit("+ record.id +")'></a>");
		}else{
			str.push("<span style='margin-left:16px'></span>");
		}
		if(Ccay.Permission.check("userManagement:delete") && !record.deleted){
			str.push("<a class='ccay-icon del'  title='"+ $i18n("ccay.common.button.delete")
				+ "' onClick='ccay.security.user.list.toDelete("+ record.id +")'></a>");
		}else{
			str.push("<span style='margin-left:16px'></span>");
		}
		if(Ccay.Permission.check("userManagement:revert") && record.deleted){
			str.push("<a class='ccay-icon reset'  title='"+ $i18n("ccay.common.button.revert")
				+ "' onClick='ccay.security.user.list.toRevert("+ record.id +")'></a>");
		}else{
			str.push("<span style='margin-left:16px'></span>");
		}
		
		return str.join("");
	};
	
	var gridOptions={
			wrapHead:false,
			allowSort:false,
			showSeq:false,
			showPager : true,
			percentWidth:true,
			customSetting: true,
			pagerOp:{
				pageInf: {
					pageSize: 15,
					curPage: 1
				},
				pageSizes: [15, 20, 50, 100, 200, 500]
			}, 
	
			columns:[
				 {field: 'id',header: "ID",hidden:true},
				 {freezeable : true,columnType: 'operate',content:"${#userOp(data)}",width:"15%"},
			     {field:'name',header: "ccay.common.label.username",align:"left", width:"15%"},
			     {field: 'displayName' , header: "ccay.common.label.userDisplayName",align:"left", width:"15%"},
			     {field: 'email', header:"ccay.common.label.email",align:"left", width:"15%"},
			     {field: 'phone', header:"ccay.common.label.phone",align:"left", width:"15%"},
			     {field: 'expireDate', header:"ccay.common.label.expireDate",align:"left", width:"15%", type: 'date', format: {dateFormat:'yyyy-MM-dd'}},
			     {field: 'enabled', header:"ccay.common.label.isValid",align:"center", width:"15%",type:'enum',format:{service: 'ENUM:EnableEnum'}},
			     {field: 'deleted', header:"ccay.common.label.isDeleted",align:"center", width:"15%",type:'enum',format:{service: 'ENUM:YesNoEnum'}}
			],
			fnBeforeRequest : function(opt){
		    	opt.ajax.data.dimensions = Program.getDimensionJson("programItemSearchId");
		    },
			ajax:{
				type:'POST',
				url:'services/manage/security/userManage/findPagedUsers/{0}/{1}'
		    },
			operation:{
				search:{
					btn:'#btnSearch',
					panel:'#queryUserForm'
				}
			}
	  }
	
	page.ready=function(){
		$S("#listUserTab").ccayTab();
		Program.renderItems("programItemSearchId","search");
		$S("#roleId").ccaySuggest(Role.suggestRoleEditOpt());
		$S("#programId").ccayPopBox(Program.suggestPopupEditOpt());
		$S("#userGrid").igrid(gridOptions);
		igridObj = $S("#userGrid").manager();
	}
	
	/**
	 * 重新查询
	 */
	page.research = function(){
		igridObj.reload(true,true,true);
	}
	
	/**
	 * 授权
	 */
	page.openPermission = function(userId){
		Ccay.Page.register("ccay.security.user.EditAuthorization2User","manage/security/user/editAuthorization2User.html");
		Ccay.Common.openPage("ccay.permission.label.addPermission",
				"ccay.security.user.EditAuthorization2User", {
					tab : '#listUserTab'
				}, function() {
					ccay.security.user.EditAuthorization2User.init(""+userId);
				});
	}
	
	
	/**
	 * 关闭其他也签
	 */
	page.closeTab = function(isReload) {
		var listRoleTabLength = $('#listUserTab').length;
		if(listRoleTabLength){
			$('#listUserTab').ccayTabRemoveItem();
			if (isReload){
				igridObj.reload(true,true,true);
			}
		}
	};
	
	/**
	 * 删除用户
	 */
	page.toDelete = function(userId){
		Ccay.doDelete({
 			async:false,
 			url:"services/manage/security/userManage/delete/"+userId,
 			data :{id:userId},
 			success:function(data){
 				$S("#btnSearch").click();
 			}			
 		});
	}
	/**
	 * 恢复已删除用户
	 */
	page.toRevert = function(userId){
		Ccay.doPut({
 			async:false,
 			url:"services/manage/security/userManage/revert/"+userId,
 			data : "",
 			success:function(data){
 				$S("#btnSearch").click();
 			}			
 		});
	}
	/**
	 * 添加或编辑窗口
	 */
	page.toEdit = function(userId) {
		//注册页面资源
		Ccay.Page.register("ccay.security.user.edit","manage/security/user/userEdit.html");
		// 注册弹出框
		Ccay.Common.openPage("ccay.common.button.edit","ccay.security.user.edit", {
					width : 400,
					height : 300,
					buttons : [{
						text : "ccay.common.button.save",
						onClick : function() {
							// 保存方法
							ccay.security.user.edit.save();
						}
					}, {
						text : "ccay.common.label.close",
						onClick : function() {
							// 关闭方法
							ccay.security.user.edit.close();
						}
					}]
				}, function() {
					// 初始化方法
					ccay.security.user.edit.init(userId);
				});
	}
	
	/**
	 * 普通上传
	 */
	page.userImport = function() {
		//导入标识
		var importLogo = $S("#opImportLogo").ccayGetValue();
		var param = {
				uploadType : "ExcelImport",
				completeCallback : Ccay.Component.Excel.uploadCallback,
				closeDialog : true,
				query : "excelType=eamsFds.archive.batchVolumeArch&importLogo="+importLogo + "&templateType=1&operationType=1",
				maxFileCount : 1
			};
		Ccay.Component.openUploadDialog("eams.arc.compileinbatches", param);
	}
});